package org.example.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.ex.handler.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mapper.MemberAddrMapper;
import org.example.domain.MemberAddr;
import org.example.service.MemberAddrService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@CacheConfig(cacheNames = "org.example.service.Impl.MemberAddrServiceImpl")
public class MemberAddrServiceImpl extends ServiceImpl<MemberAddrMapper, MemberAddr> implements MemberAddrService{

    @Autowired
    private MemberAddrMapper memberAddrMapper;

    @Override
    @Cacheable(key = "#openId")
    public List<MemberAddr> queryMemberAddrListByOpenId(String openId) {
        return memberAddrMapper.selectList(new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId)
                    .eq(MemberAddr::getStatus,1)
                .orderByDesc(MemberAddr::getCommonAddr,MemberAddr::getCreateTime));
    }

    @Override
    @CacheEvict(key = "#openId")
    public Boolean saveMemberAddr(MemberAddr memberAddr,String openId) {

        // 补充收货地址信息
        memberAddr.setOpenId(openId);
        memberAddr.setCommonAddr(0);
        memberAddr.setStatus(1);
        memberAddr.setCreateTime(new Date());
        memberAddr.setUpdateTime(new Date());

        // 根据会员openId查询会员收货地址数量
        Long count = memberAddrMapper.selectCount( new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId));

        // 判断会员是否有收货地址
        if (0 == count) {
            // 说明：当前会员新增的收货地址为第1个即默认收货地址
            memberAddr.setCommonAddr(1);
        }

        return memberAddrMapper.insert( memberAddr) > 0;

    }

    @Override
    @CacheEvict(key = "#openId")
    public Boolean modifyMemberAddrInfo(MemberAddr memberAddr, String openId) {
        memberAddr.setUpdateTime(new Date());
        return memberAddrMapper.updateById( memberAddr) > 0;
    }

    @Override
    @CacheEvict(key = "#openId")
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeMemberAddrById(Long addrId, String openId) {
        MemberAddr memberAddr = memberAddrMapper.selectById( addrId);
        Long allMemberAddr = memberAddrMapper.selectCount( new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId)
                .eq(MemberAddr::getStatus,1));
        //判断该地址是否是默认地址
        if (memberAddr.getCommonAddr().equals(1)) {
            //选取该openId地址的其他随机一个地址作为默认地址
            //判断该openId地址数量
            if ( allMemberAddr > 1 ){
                List<MemberAddr> memberAddrList =memberAddrMapper.selectList( new LambdaQueryWrapper<MemberAddr>()
                        .eq(MemberAddr::getOpenId, openId)
                        .ne(MemberAddr::getAddrId, addrId)
                        .eq(MemberAddr::getStatus,1)
                        .eq(MemberAddr::getCommonAddr,0)
                        .orderByDesc(MemberAddr::getCommonAddr,MemberAddr::getCreateTime));
                // 判断非默认收货地址是否有值
                if (CollectionUtil.isNotEmpty( memberAddrList) && memberAddrList.size() != 0) {
                    // 说明：会员有非默认收货地址 -> 获取第1个地址并设置为新的默认收货地址
                    MemberAddr newDefaultMemberAddr = memberAddrList.get(0);
                    newDefaultMemberAddr.setCommonAddr(1);
                    newDefaultMemberAddr.setUpdateTime(new Date());
                    memberAddrMapper.updateById(newDefaultMemberAddr);
                }
            }else{
                throw new BusinessException("您没有其他收货地址，只有该收货地址，请增加新的收货地址!");
            }
        }

        return memberAddrMapper.deleteById( addrId) > 0;
    }

    @Override
    @CacheEvict(key = "#openId")
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyMemberDefaultAddr(String openId, Long newAddrId) {
        // 获取默认收货地址
        MemberAddr memberAddr = memberAddrMapper.selectOne( new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getCommonAddr,1));
        // 判断该地址是否是默认地址
        if(ObjectUtil.isNotEmpty(memberAddr) && memberAddr.getAddrId().equals(newAddrId)){
            //是 则不做操作
            return true;
        }
        memberAddr.setCommonAddr(0);
        memberAddr.setUpdateTime(new Date());
        memberAddrMapper.update(memberAddr, new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId,openId));
        //根据newAddrId 查询地址数据
        MemberAddr newMemberAddr = memberAddrMapper.selectById( newAddrId);
        newMemberAddr.setCommonAddr(1);
        newMemberAddr.setUpdateTime(new Date());

        return memberAddrMapper.update(newMemberAddr, new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId,openId)) > 0;
    }
}
